
/**
 * @private
 * @description Engine to render an abstract data to pure HTML
 * @constructor
 * @class
 * @version 0.1a
 */ 
function SntPureHtmlEmbed(){
	this.classid = "clsid:D27CDB6E-AE6D-11cf-96B8-444553540000";
	this.type = 'application/x-shockwave-flash';
	this.codebase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0'
	this.bf = new browserInfo().process();
	this.browser = null;
	// error status, default is fine
	this.status = true;
	this.callback = null;
	return this;
};

SntPureHtmlEmbed.prototype = {	
	/**
	 * @function
	 * @description Add one param element to object
	 * @param o object instance
	 * @param name name of attribute
	 * @param value value of the attribute
	 */
	createParam : function(o, name, value){
		var p = document.createElement('param');
		p.setAttribute('name', name);
		p.setAttribute('value', value);
		o.appendChild(p);
	},
	/**
	 * @function
	 * @description Set callback function
	 * @param fn callback function or null
	 * @return void
	 */
	setCallback : function(fn){
		this.callback = fn;
	},
	/**
	 * @function
	 * @description Create and object element
	 * @param swf path to swf movie
	 * @param attrs array with attributes for the object element
	 * @param params list of params for the object element such as AllowFullScreen etc.
	 * @return HTML DOM element
	 */
	createObject : function(swf, size, attrs, params){
		var o = document.createElement("object");
		
		// set class id, required attribute for IE		
		o.setAttribute('classid', this.classid);
		o.setAttribute('type', this.type);
		o.setAttribute('codebase', this.codebase);
		
		o.setAttribute('width', size.width);
		o.setAttribute('height', size.height);
		
		// set path to swf
		params['movie'] = swf;
		
		// set attributes
		for(var a in attrs){
			if(a == 'name')continue;
			if(a == 'styleclass'){
				o.setAttribute('class', attrs[a]);
				continue;
			}
			o.setAttribute(a, attrs[a]);			
		}
		
		// set params
		for(var p in params){
			this.createParam(o, p, params[p]);
		}
	

		
		var embed = this.createEmbed(swf, size, attrs, params);
		// if browser is IE7 just return embed	
		if(this.bf.isBrowser('IE_7') == true){				
			// fix name attribute for IE7
			return [o, embed.outerHTML.replace('<EMBED ', '<EMBED name="' + attrs['name'] + '" ')];	
		}else{
			o.appendChild(embed);
		}
		
		return o;
	},
	
	/**
	 * @functions
	 * @description create an embed element into object
	 * @param swf path to flash movie
	 * @param size array with size of the flash object (associate array with keys <b>width</b> and <b>height</b>)
	 * @param attrs array with attributes for the embed element	 
	 * @param params array with params (for embed it's attributes too) for the embed element
	 * @return void
	 */
	createEmbed : function(swf, size, attrs, params){

	 	// set path to swf
		attrs['src'] = swf;
		embed = document.createElement('embed');
		embed.setAttribute('type', this.type);
		
		embed.setAttribute('width', size.width);
		embed.setAttribute('height', size.height);
		
		if(attrs['id'])delete attrs['id'];			
				
		// set all params as embed's attributes
		for(var p in params){
			embed.setAttribute(p, params[p]);
		}

		// set all of attributes
		for(var a in attrs){
			embed.setAttribute(a, attrs[a]);
		}
		return embed;
	},
	/**
	 * @function
	 * @private
	 * @description render flashvars array to GET-like request string
	 * @param fv an array with flashvars
	 * @return string
	 */
	renderFlashvars : function(fv){
		var tmp = [];
		for(var n in fv){ tmp[tmp.length] = escape(n) + '=' + escape(fv[n]); }
		return tmp.join('&');
	},
	
	/** 
	 * @function
	 * @description Render all received data to object element with required params.
	 *				This function used in SntEmbed#render 
	 * @param id Id of the holder
	 * @param swfMovie path to the swf 
	 * @param size an array with two elements `width` and `height` with video holder size
	 * @param flashvars array with flashvars
	 * @param params array with parameters
	 * @param attributes an array with attributes
	 * @render status of the rendering
	 */
	render : function(id, swfMovie, size, flashvars, params, attributes){	
		var holder = $$(id);
		if(holder == null){
			return false;			
		}
				
		this.browser = new browserInfo().process();
		// change holder id to temporary id
		holder.setAttribute('id', id + 'Temp');

		// render flashvars to one string		
		params['flashvars'] = this.renderFlashvars(flashvars);
		
		// replace object with id temporary id to flash object element
		var parent = holder.parentNode;
		var embed = this.createObject(swfMovie, size, attributes, params);
		if(this.bf.isBrowser('IE_7') == true){	
			parent.innerHTML(embed[1]);		
			parent.appendChild(embed[0]);

			//parent.innerHTML = embed;
		}else{
			parent.appendChild(embed);
			parent.removeChild(holder);	
		}
		alert(parent.outerHTML);

		if(this.callback != null)this.callback($$(id));
		return this.status;
	}	
};

